// const util = require('../../utils/util.js');
const util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['', '', ''],
    isflag:0,
    address: { name: '', phone: '', province: '', city: '', district:'',address:''},
    addressid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    // var user = wx.getStorageSync("userinfo");
    // var userid = user.id;
    var data = { userid: app.globalData.userInfo.id, addressid:id};
    var that = this;
    util.commonRequest("index/Address/findAddress", 'post',data, function (res) {
      // debugger
      if (res.data.code == 200) {
        var tempregion = [res.data.data.province, res.data.data.city, res.data.data.district];
        that.setData({
          editData:res.data.data,
          region: tempregion,
          isflag: 1,
          addressid:id
        })
        // debugger
      } else {
        wx.showToast({
          icon: "none",
          title: '服务器处理异常',
        })
      }
    });
  },
 
  bindRegionChange: function (e) {
    var value = e.detail.value;
    var address = this.data.address;
    address.province = value[0];
    address.city = value[1];
    address.district = value[2];
    var tempaddress = address;
    this.setData({
      region: e.detail.value,
      isflag:1,
      address: tempaddress
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  formSubmit:function(e){
    // var user = wx.getStorageSync("userinfo");
    var data = e.detail.value;
    data.province = data.area[0];
    data.city = data.area[1];
    data.district = data.area[2];
    data.userid = app.globalData.userInfo.id;
    data.addressid = this.data.addressid;
    var flag = this.validation(data);
    if(flag == 1){
      util.commonRequest("index/Address/modifyAddress",'post', data, function (res) {
        if(res.data.code==200){
          wx.showToast({
            title: '编辑成功',
          })
          wx.navigateBack({
            delta: 1
          })
        }else{
          wx.showToast({
            icon:"none",
            title: '服务器处理异常',
          })
        }
      });
    }
  },
  validation:function(data){
    if (!data.name) {
      wx.showToast({
        icon: "loading",
        title: '收货人不能为空',
      })
      return 0;
    }
    if (!data.phone) {
      wx.showToast({
        icon: "loading",
        title: '联系电话不能为空',
      })
      return 0;
    }
    if (!data.province) {
      wx.showToast({
        icon: "loading",
        title: '请选择地区',
      })
      return 0;
    }
    if (!data.address) {
      wx.showToast({
        icon: "loading",
        title: '详细地址不能为空',
      })
      return 0;
    }
    return 1;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})